package cn.edu.java.io;

import java.io.*;
import java.util.Properties;

public class PropertiesDemo {
    public static void main(String[] args) {
        try {
            // 创建Properties对象
            Properties properties = new Properties();

            // 设置属性值
            properties.setProperty("url", "localhost:8080");
            properties.setProperty("username", "root");
            properties.put("password", "123456");
            properties.setProperty("timeout", "5000");

            // 存储属性文件
            PrintWriter writer = new PrintWriter("input/properties.properties");
            properties.store(writer, "store properties");
            writer.close();

            // 读取属性文件
            FileReader reader = new FileReader("input/properties.properties");
            properties.load(reader);
            reader.close();

            // 获取属性值
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            String timeout = (String) properties.get("timeout");

            System.out.println("URL: " + url);
            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
            System.out.println("timeout: " + timeout);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

